package com.ysd.lis.service.samsto.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.lis.mapper.samsto.SamstoSpecboxOrderMapper;
import com.ysd.lis.entity.samsto.SamstoSpecboxOrder;
import com.ysd.lis.service.samsto.SamstoSpecboxOrderService;
import com.ysd.lis.request.samsto.SamstoSpecboxOrderParam;
import org.springframework.stereotype.Service;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ysd.common.Result;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ysd.lis.entity.SysUser;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仓位表(SamstoSpecboxOrder)表服务实现类
 *
 * @author leiming
 * @since 2024-10-30 15:52:57
 */
@Service("samstoSpecboxOrderService")
public class SamstoSpecboxOrderServiceImpl extends ServiceImpl<SamstoSpecboxOrderMapper, SamstoSpecboxOrder> implements SamstoSpecboxOrderService {

    private MPJLambdaWrapper<SamstoSpecboxOrder> initWrapper(){
        MPJLambdaWrapper<SamstoSpecboxOrder> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        		//ID 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getId, SamstoSpecboxOrder::getId);
		//标本ID 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getSpecimenId, SamstoSpecboxOrder::getSpecimenId);
		//标本盒表 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getSpecboxId, SamstoSpecboxOrder::getSpecboxId);
		//库位号 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getOrderNumber, SamstoSpecboxOrder::getOrderNumber);
		//机构 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getOrgId, SamstoSpecboxOrder::getOrgId);
		//存储状态 0为空，1为放置 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getEmpty, SamstoSpecboxOrder::getEmpty);
		//状态 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getStatus, SamstoSpecboxOrder::getStatus);
		//${column.comment} 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getDeptName, SamstoSpecboxOrder::getDeptName);
		//删除标记 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getDelFlag, SamstoSpecboxOrder::getDelFlag);
		//创建人 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getCreator, SamstoSpecboxOrder::getCreator);
		//创建时间 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getCreateTime, SamstoSpecboxOrder::getCreateTime);
		//修改人 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getEditor, SamstoSpecboxOrder::getEditor);
		//修改时间 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getEditTime, SamstoSpecboxOrder::getEditTime);
		//删除时间 
		lambdaQueryWrapper.selectAs(SamstoSpecboxOrder::getDelTime, SamstoSpecboxOrder::getDelTime);
        return lambdaQueryWrapper;
    }

    @Override
    public Result findPageList(SamstoSpecboxOrderParam pageParam) {
        Page<SamstoSpecboxOrder> pageList=null;
        LambdaQueryWrapper<SamstoSpecboxOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<SamstoSpecboxOrder> page = new Page<SamstoSpecboxOrder>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        lambdaQueryWrapper.eq(SamstoSpecboxOrder::getDelFlag, 0);
        /*if (StringUtils.isNotBlank(pageParam.getKeywords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(SamstoSpecboxOrder::getBoxDoor, pageParam.getKeywords())
                    .or().like(SamstoSpecboxOrder::getModelName, pageParam.getKeywords())
                    .or().like(SamstoSpecboxOrder::getBrandName, pageParam.getKeywords()));
        }*/
        lambdaQueryWrapper.orderByAsc(SamstoSpecboxOrder::getCreateTime);
        
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                lambdaQueryWrapper.eq(SamstoSpecboxOrder::getOrgId, sysUser.getOrgId());
            }
            pageList= this.baseMapper.selectPage(page,lambdaQueryWrapper);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Result.succ(1, "获取信息成功", pageList);
    }

    @Override
    public List<SamstoSpecboxOrder> findListBySpecboxId(String specboxId) {
        List<SamstoSpecboxOrder> specboxOrders=null;
        LambdaQueryWrapper<SamstoSpecboxOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(SamstoSpecboxOrder::getId);
        lambdaQueryWrapper.eq(SamstoSpecboxOrder::getSpecboxId,specboxId);
        lambdaQueryWrapper.eq(SamstoSpecboxOrder::getStatus,1);
        lambdaQueryWrapper.eq(SamstoSpecboxOrder::getDelFlag,0);
        specboxOrders= this.baseMapper.selectList(lambdaQueryWrapper);
        return specboxOrders;
    }

    @Override
    public Result getInfoById(String id) {
        SamstoSpecboxOrder SamstoSpecboxOrder=this.baseMapper.selectById(id);
        return Result.succ(1, "获取信息成功", SamstoSpecboxOrder);
    }

    @Override
    public Result add(SamstoSpecboxOrderParam param) {
        SamstoSpecboxOrder info=new SamstoSpecboxOrder();
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                param.setCreator(sysUser.getId());
                param.setOrgId(sysUser.getOrgId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        param.setCreateTime(new Date());
        BeanUtil.copyProperties(param,info);
        info.setDelFlag(0);
        info.setStatus(1);
        this.baseMapper.insert(info);
        return Result.succ(1, "添加信息成功", null);
    }

    @Override
    public Result edit(SamstoSpecboxOrderParam param) {
        Map returnMap = new HashMap();
        SamstoSpecboxOrder info=new SamstoSpecboxOrder();
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                param.setEditor(sysUser.getId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        param.setEditTime(new Date());
        BeanUtil.copyProperties(param,info);
        this.baseMapper.updateById(info);
        returnMap.put("id", param.getId());
        return Result.succ(1, "修改信息成功", returnMap);
    }

    @Override
    public Result delete(List<String> ids) {
        Result result;
        try {
            boolean b = this.removeByIds(ids);
            if (b) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

    @Override
    public Result updelete(List<String> ids) {
        Result result;
        LambdaUpdateWrapper<SamstoSpecboxOrder> lambdaUpdateWrapper = new UpdateWrapper<SamstoSpecboxOrder>().lambda();
        // set 表示要更新的字段， eq 表示  WHERE 条件
        lambdaUpdateWrapper.set(SamstoSpecboxOrder::getDelFlag,1)
                .in(SamstoSpecboxOrder::getId, ids);
        this.baseMapper.update(new SamstoSpecboxOrder(), lambdaUpdateWrapper);

        try {
            int b= this.baseMapper.update(new SamstoSpecboxOrder(), lambdaUpdateWrapper);
            if (b>0) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }
    
    @Transactional
    @Override
    public Result saveOrUpdateList(SamstoSpecboxOrderParam param) {
        Result result;
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isEmpty(sysUser)) {
               return Result.fail(304, "请重新登陆", "");
            }
            for (SamstoSpecboxOrder info : param.getInfoList()) {
                if(StringUtils.isNotBlank(info.getId())){
                    info.setEditor(sysUser.getId());
                    info.setEditTime(new Date());
                    this.baseMapper.updateById(info);
                }else{
                    info.setCreator(sysUser.getId());
                    info.setOrgId(sysUser.getOrgId());
                    info.setCreateTime(new Date());
                    info.setDelFlag(0);
                    info.setStatus(1);
                    this.baseMapper.insert(info);
                }
            }
            Boolean b= this.saveOrUpdateBatch(param.getInfoList());
            if (b) {
                result = Result.succ(1, "保存成功", null);
            } else {
                result = Result.fail(400, "保存失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "保存失败", "");
        }
        return result;
    }
    @Override
    public int getCount(List<String> specboxids) {
        int count=0;
        LambdaQueryWrapper<SamstoSpecboxOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SamstoSpecboxOrder::getSpecboxId,specboxids);
        lambdaQueryWrapper.eq(SamstoSpecboxOrder::getEmpty,1);
        count=this.baseMapper.selectCount(lambdaQueryWrapper);
        return count;
    }
}

